package com.sunnybear.framework.ui;

import android.text.Html;
import android.text.TextUtils;
import android.webkit.WebView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.sunnybear.framework.ui.recyclerview.QuickRecyclerView;

import java.util.List;

import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 控件绑定适配器
 *  2018/4/4.
 */
public final class WidgetBindingAdapter {

    /**
     * 转换HTML绑定到TextView
     *
     * @param textView
     * @param html
     */
    @BindingAdapter(value = {"android:html"})
    public static void htmlToText(TextView textView, String html) {
        if (!TextUtils.isEmpty(html))
            textView.setText(Html.fromHtml(html));
    }

    /**
     * WebView加载HTML片段
     *
     * @param webView
     * @param html
     */
    @BindingAdapter(value = {"android:html"})
    public static void loadHtml(WebView webView, String html) {
        webView.getSettings().setDefaultTextEncodingName("UTF-8");
        webView.loadData(html, "text/html", "UTF-8");
    }

//    /**
//     * 加载网络图片
//     *
//     * @param url            图片网络地址
//     * @param placeholder    加载占位图
//     * @param error          加载错误图
//     * @param transformation 转换器
//     */
//    @BindingAdapter(value = {
//            "android:imageUrl",
//            "android:placeholder",
//            "android:error",
//            "android:transformation"
//    }, requireAll = false)
//    public static void imageLoader(ImageView imageView, String url, int placeholder, int error, Transformation<Bitmap> transformation) {
//        //加载动画
//        final ObjectAnimator animator = ObjectAnimator.ofInt(imageView, "ImageLevel", 0, 10000);
//        animator.setDuration(500);
//        animator.setRepeatCount(ObjectAnimator.INFINITE);
//        animator.start();
//
//        GlideRequests requests = ImageLoader.with(imageView.getContext());
//        if (requests != null && !TextUtils.isEmpty(url)) {
//            RequestBuilder builder = requests.load(url)
//                    .placeholder(placeholder).error(error)
//                    .listener(new RequestListener<Drawable>() {
//                        @Override
//                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                            animator.cancel();
//                            Logger.d("Glide", "图片加载失败");
//                            return false;
//                        }
//
//                        @Override
//                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                            animator.cancel();
//                            Logger.d("Glide", "图片加载成功");
//                            return false;
//                        }
//                    });
//            if (transformation != null)
//                builder = builder.apply(RequestOptions.bitmapTransform(transformation));
//            builder.transition(DrawableTransitionOptions.withCrossFade(500)).into(imageView);
//        }
//    }

    /**
     * BottomNavigationBar的栏位设置
     *
     * @param bottomNavigationBar
     * @param items
     */
    @BindingAdapter(value = {"android:bottomNavigationItems"})
    public static void setBottomNavigationItems(BottomNavigationBar bottomNavigationBar, List<BottomNavigationItem> items) {
        for (BottomNavigationItem item : items) {
            bottomNavigationBar.addItem(item);
        }
        bottomNavigationBar.setFirstSelectedPosition(0).initialise();
    }

    @BindingAdapter(value = {"android:itemDecoration"})
    public static void addRecyclerViewItemDecoration(QuickRecyclerView quickRecyclerView, RecyclerView.ItemDecoration itemDecoration) {
        if (itemDecoration != null)
            quickRecyclerView.addItemDecoration(itemDecoration);
    }

    @BindingAdapter(value = {"android:adapter"})
    public static void addRecyclerViewAdapter(QuickRecyclerView quickRecyclerView, DelegateAdapter.Adapter adapter) {
        if (adapter != null)
            quickRecyclerView.addAdapter(adapter);
    }
}
